package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.dam.api.DBDictService;
import sm.upgrade.api.DataService;
import sm.upgrade.api.DataUpgrade;
import sm.upgrade.api.DataUpgradeService;
import sm.upgrade.exception.UpgradeException;
import sm.upgrade.util.UpgradeContext;

public class DataUpgradeImpl implements DataUpgrade {

	private static Logger logger = LoggerFactory.getLogger(DataUpgrade.class);

	private HibernateTemplate upgradeTemplate;

	private DataUpgradeService dataUpgradeService;

	private DataService upgradeDataService;

	private DataService baseDataService;

	public DataUpgradeImpl(ApplicationContext customerContext,
			ApplicationContext upgradeContext, ApplicationContext baseContext) {
		dataUpgradeService = (DataUpgradeService) customerContext
				.getBean("dataUpgradeService");
		upgradeTemplate = (HibernateTemplate) upgradeContext
				.getBean("template");
		upgradeDataService = (DataService) upgradeContext
				.getBean("dataService");

		if (baseContext != null) {
			baseDataService = (DataService) baseContext.getBean("dataService");
		}
	}

	public DataUpgradeImpl() {
		this(UpgradeContext.getInstance().getOrCreateCustomerContext(),
				UpgradeContext.getInstance().getOrCreateUpgradeContext(),
				UpgradeContext.getInstance().getOrCreateBaseContext());
	}

	@Override
	public void upgradeData() {
		logger.info("Start DATA PHASE");
		List<Object> list = upgradeTemplate
				.find("select name from dbdict where name != ?",
						DBDictService.DBDICT);
		for (Object obj : list) {
			String fileName = (String) obj;
			try {
				upgradeData(fileName);
			} catch (Exception e) {
				logger.error(
						"Error in upgrade data " + fileName + ":"
								+ e.getMessage(), e);
			}
		}
		logger.info("  Finish DATA PHASE");
	}

	@Override
	public void upgradeData(String fileName) throws UpgradeException {
		dataUpgradeService.upgradeData(fileName, upgradeDataService,
				baseDataService);

	}

	@Override
	public void show2wayConflict() {
		showConflict(false);
	}

	@Override
	public void show2wayConflict(String fileName) {
		dataUpgradeService.showConflict(fileName, upgradeDataService, null);

	}

	@Override
	public void show3wayConflict() {
		showConflict(true);
	}

	@Override
	public void show3wayConflict(String fileName) {
		dataUpgradeService.showConflict(fileName, upgradeDataService,
				baseDataService);
	}

	private void showConflict(boolean use3way) {
		logger.info("Begin check data conflicts");
		List<Object> list = upgradeTemplate.find(
				"select name from dbdict where name != ?", "dbdict");
		for (Object obj : list) {
			String fileName = (String) obj;
			if (use3way) {
				show3wayConflict(fileName);
			} else {
				show2wayConflict(fileName);
			}
		}
		logger.info("End check data conflicts");
	}

}
